package com.lz.football.module.techBar.view
{
	import com.common.stdlib.DateUtil;
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.common.utils.timeUtils.TimerManager;
	import com.global.GlobalDef;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.techBar.vo.TechBarInfoVo;
	import com.liangzhong.model.type.BuildingType;
	import com.lz.football.module.techBar.ITechBarView;
	import com.lz.football.module.techBar.type.TechType;
	
	import common.BaseView.DisplayMouseEventManager;
	import common.comp.TabButtonBar;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.LButton;
	import lz.controls.core.TitleWindow;
	import lz.controls.managers.PopUpManager;

	/**
	 *科技馆 
	 * @author lake
	 * 
	 */	
	public class TechBarView extends TitleWindow implements ITechBarView
	{
	
		/**
		 *关闭按钮 
		 */		
		public static var CLoseBtn	 		:SimpleButton;
		/**
		 *选项卡 
		 */		
		private var buttonBar 				:TabButtonBar;
		/**
		 *容器 
		 */		
		private var container				:MovieClip;
		/**
		 *当前显示的面板 
		 */		
		private var curPanel				:DisplayObject;
		/**
		 * 装备合成
		 */		
		private var equipCombinePanel  	:EquipCombinePanel;
		/**
		 *材料合成 
		 */		
		private var materialCombinePanel 	:MaterialCombinePanel;
		/**
		 *球员合成 
		 */		
		private var playerCombinePanel		:PlayerCombinePanel;
		/**
		 *球衣合成 
		 */		
		private var clothCombinePanel		:ClothCombinePanel;
		/**
		 *角色转职 
		 */		
		private var roleChangePanel		:RoleChangePanel;
		/**
		 *球员洗练 
		 */		
		private var playerRefreshPanel		:PlayerRefreshPanel;
		/**
		 *角色洗练 
		 */		
		private var roleRefreshPanel		:RoleRefreshPanel;
		/**
		 *规则 
		 */		
		private var techRulePanel			:TechRulePanel;
		
		public function TechBarView()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.TechBarSkin");
			this.addChild(_symbolSkin);
			//初始化宽高
			width = _symbolSkin.width;
			height = _symbolSkin.height;
			//容器
			container = new MovieClip();
			_symbolSkin.addChild(container);
			//初始化UI界面控件
			windowBg = _symbolSkin.windowBg;//背景框
			//选项bar 初始化
			buttonBar = new TabButtonBar();
			var i :int = 1;
			//等级限制
			var lvLimits :Array = [12,16,22,26];
			var openIndexs :Array = [4,5,6,7];
			var tipsInfos :Array = ["俱乐部等级12级开放","俱乐部等级16级开放","俱乐部等级22级开放","俱乐部等级26级开放"];
			var openIndex :int = 8;
			var clubLv :int = AppData.getInstance().clubDatamanager.clubInfo.level;
			for(i = 0;i<=3;i++)
			{
				if(clubLv < lvLimits[i])
				{
					openIndex = openIndexs[i];
					break;
				}
			}
			//标签初始化
			var lbtn :LButton;
			var lbtns :Array = [];
			for(i = 1;i<=7;i++)
			{
				lbtn = new LButton(_symbolSkin["buttonBar"]["skin_" + i]);
				if(i < openIndex)
				{
					lbtn.enable = true;
				}
				else
				{
					lbtn.enable = false;
					lbtn.setStatus(LButton.GRAY);
					DisplayMouseEventManager.instance.addTipsDisplay(lbtn.view,tipsInfos[i-4]);
				}
				lbtns.push(lbtn);
			}
			buttonBar.setButtons(lbtns);
			buttonBar.selectTabButton(lbtns[0]);
			//设置可拖动
			dragEnabled = true;
			//设置关闭按钮
			setCloseBtn(_symbolSkin.closeBtn);
			//设置坐标不自适应
			resizeEnabled = false;
			//添加事件
			addEvtListener();
			//浅入
			fadeIn();
			//显示基本信息
			showBaseInfo();
			//显示装备合成功能
			showPanelByType(TechType.ROLE_REFRESH);
			//初始化规则
			techRulePanel = new TechRulePanel();
			techRulePanel.initUI();
			//guide
			CLoseBtn = closeBtn;
		}
		/**
		 *显示基本信息
		 * 
		 */		
		private function showBaseInfo():void
		{
			
		}
		/**
		 * 去除冷却时间 确认
		 * @param type
		 * 
		 */		
		private function dropTimeAlertHandler(type :int):void
		{
			if(type == Alert.YES)
			{
				this.dispatchEvent(new ModuleEvent(ModuleEvent.DROP_TIME,true,false,BuildingType.TECH_BAR));
			}
		}
		/**
		 *注册事件 
		 * 
		 */	
		public function addEvtListener():void
		{
			buttonBar.addEventListener(Event.CHANGE,onTabChangeHandler);
			this.addEventListener("rule_Click",ruleHandler);
			AppData.getInstance().techBarManager.addEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 *数据改变 
		 * @param e
		 */		
		private function dataChangeHandler(e :Event):void
		{
			showBaseInfo();
		}
		/**
		 *选项卡改变 
		 * @param e
		 * 
		 */		
		private function onTabChangeHandler(e :Event):void
		{
			var selectType :int = buttonBar.selectType + 1;
			showPanelByType(selectType);
		}
		/**
		 *点击升级 
		 * @param e
		 * 
		 */		
		private function gradeClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new TechBarEvent(TechBarEvent.GRADE_UP));
		}
		/**
		 * 根据类型显示界面
		 */		
		public function showPanelByType(type :int):void
		{
			DisplayObjectUtil.removeAllChildren(container);
			switch(type)
			{
				case TechType.EQUIP_COMBINE://装备合成
				{
					if(equipCombinePanel == null)
					{
						equipCombinePanel = new EquipCombinePanel();
						equipCombinePanel.initUI();
					}
					curPanel = equipCombinePanel;
					break;
				}
				case TechType.MATERIAL_COMBINE://材料合成
				{
					if(materialCombinePanel == null)
					{
						materialCombinePanel = new MaterialCombinePanel();
						materialCombinePanel.initUI();
					}
					curPanel = materialCombinePanel;
					break;
				}
				case TechType.STAR_COMBINE://球员合成
				{
					if(playerCombinePanel == null)
					{
						playerCombinePanel = new PlayerCombinePanel();
						playerCombinePanel.initUI();
					}
					curPanel = playerCombinePanel;
					break;
				}
				case TechType.CLOTH_COMBINE://球衣合成
				{
					if(clothCombinePanel == null)
					{
						clothCombinePanel = new ClothCombinePanel();
						clothCombinePanel.initUI();
					}
					curPanel = clothCombinePanel;
					break;
				}
				case TechType.ROLE_CHANGE://角色转职
				{
					if(roleChangePanel == null)
					{
						roleChangePanel = new RoleChangePanel();
						roleChangePanel.initUI();
					}
					curPanel = roleChangePanel;
					break;
				}
				case TechType.PLAYER_REFRESH://球员洗练
				{
					if(playerRefreshPanel == null)
					{
						playerRefreshPanel = new PlayerRefreshPanel();
						playerRefreshPanel.initUI();
					}
					curPanel = playerRefreshPanel;
					break;
				}
				case TechType.ROLE_REFRESH://角色洗练
				{
					if(roleRefreshPanel == null)
					{
						roleRefreshPanel = new RoleRefreshPanel();
						roleRefreshPanel.initUI();
					}
					curPanel = roleRefreshPanel;
					break;
				}
			}
			if(curPanel)
			{
				curPanel.x = 6;
				curPanel.y = 99;
				container.addChild(curPanel);
			}
			if(curPanel as ITechPanel)
			{
				(curPanel as ITechPanel).reset();
			}
		}
		/**
		 * 规则 
		 * @param e
		 * 
		 */		
		private function ruleHandler(e :TechBarEvent):void
		{
			var index :int = e.param;
			techRulePanel.setFrame(index);
			if(techRulePanel.parent == null)
			{
				PopUpManager.addWindow(techRulePanel,GlobalDef.GameWindowContainer,this.x + 5,122,false);
			}
		}
		/**
		 *删除事件 
		 * 
		 */	
		public function removeEvtListener():void
		{
			buttonBar.removeEventListener(Event.CHANGE,onTabChangeHandler);
			this.removeEventListener("rule_Click",ruleHandler);
			AppData.getInstance().techBarManager.removeEventListener(Event.CHANGE,dataChangeHandler);
		}
		/**
		 * 关闭按钮响应
		 * @param e
		 * 
		 */		
		override protected function onCloseHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new ModuleEvent(ModuleEvent.REMOVE_MODULE,true));
			super.onCloseHandler(e);
		}
		/**
		 *销毁 
		 */	
		public function destroyView():void
		{
			removeEvtListener();
			buttonBar.destroyView();
			buttonBar = null;
			if(equipCombinePanel)
			{
				equipCombinePanel.destroyView();
				equipCombinePanel = null;
			}
			if(materialCombinePanel)
			{
				materialCombinePanel.destroyView();
				materialCombinePanel = null;
			}
			if(playerCombinePanel)
			{
				playerCombinePanel.destroyView();
				playerCombinePanel = null;
			}
			if(clothCombinePanel)
			{
				clothCombinePanel.destroyView();
				clothCombinePanel = null;
			}
			if(roleChangePanel)
			{
				roleChangePanel.destroyView();
				roleChangePanel = null;
			}
			if(roleRefreshPanel)
			{
				roleRefreshPanel.destroyView();
				roleRefreshPanel = null;
			}
			if(playerRefreshPanel)
			{
				playerRefreshPanel.destroyView();
				playerRefreshPanel = null;
			}
			if(techRulePanel.parent)
			{
				techRulePanel.parent.removeChild(techRulePanel);
			}
			techRulePanel.destroyView();
			techRulePanel = null;
			CLoseBtn = null;
			curPanel = null;
		}
		/**
		 *显示tab类型 
		 * @param $obj
		 */
		public function showType($obj:Object):void
		{
		}
		//////////////////////implements///////////////////////////
		/**
		 *升级 
		 */		
		public function gradeUp():void
		{
			showBaseInfo();
		}
		/**
		 *去除时间 
		 * 
		 */		
		public function dropTime():void
		{
			showBaseInfo();
		}
		/**
		 *装备合成 
		 * 
		 */		
		public function equipCombine(value :Object):void
		{
			equipCombinePanel.equipCombine(value);
		}
		/**
		 *材料合成 
		 * 
		 */		
		public function materialCombine(value :Object):void
		{
			materialCombinePanel.materialCombine(value);
		}
		/**
		 *球员合成 
		 * 
		 */		
		public function playerCombine(value :Object):void
		{
			playerCombinePanel.playerCombine(value);
		}
		/**
		 *球衣合成 
		 * 
		 */		
		public function clothCombine(value :Object):void
		{
			clothCombinePanel.clothCombine(value);
		}
		/**
		 *球员洗练
		 * 
		 */		
		public function playerRefresh(value :Object):void
		{
			playerRefreshPanel.playerRefresh(value);
		}
		/**
		 *角色转职
		 * 
		 */		
		public function roleChange(value :Object):void
		{
			roleChangePanel.roleChange(value);
		}
		/**
		 *角色强化
		 * 
		 */		
		public function roleRefresh(value :Object):void
		{
			roleRefreshPanel.roleRefresh(value);
		}
	}
}